// pages/getFlowers/uploadVideo/uploadVideo.js
const service = require('./uploadVideoService.js');
const babyService = require('../../getBaby/getBabyService.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    execData: null,
    myVidios: '',    // 视频地址
    index: 0,
    checked: false,
    cards: ['/images/mine/card1.jpg', '/images/mine/card1.jpg', '/images/mine/card1.jpg'],
    cardIdx: 0,
    banners: [],//感谢卡
    maxDuration: 60,
    count: 0,
    textValue: '',
    order: null,//订单页传过来的数据
    good: null
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  onReady: function (options) {
    this.dialog = this.selectComponent("#maskView");
    this.requestBabyList();
    this.requestThankCardList();
  },

  onLoad: function (options) {
    if (options.order) {
      this.setData({
        order: JSON.parse(options.order),
        good: JSON.parse(options.order)
      })
    }
  },

  /**
   * 上传视频
   */
  addVideoClick: function () {
    var that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      camera: 'back',
      maxDuration: that.data.maxDuration,
      success: function (res) {
        if (res.duration > that.data.maxDuration) {
          wx.showToast({
            title: '最长录制时间' + that.data.maxDuration + '秒',
            duration: 2000,
            icon: 'none'
          });
          return;
        }
        console.log(res)
        that.setData({
          myVidios: res.tempFilePath
        })
      }
    })
  },

  /**
     * 移除视频
     */
  removeVideo: function (e) {
    this.data.myVidios = '';
    this.setData({
      myVidios: this.data.myVidios
    })
    console.log(this.data.myVidios)
  },

  /**
   * 选择宝贝
   */
  locationClick: function (e) {
    var that = this;
    let data = JSON.stringify(this.data.execData);
    wx.navigateTo({
      url: '../myGoodsList/myGoodsList?data=' + data,
    })
  },

  showTost: function (str) {
    wx.showToast({
      title: str,
      icon: 'succes',
      duration: 1000,
      mask: true
    })
  },
  bindFormSubmit: function (e) {
    // 获取表单id
    let formId = e.detail.formId;
    getApp().saveUserFormId(formId, 11);
    this.sendThanks(e);
  },
  /**
   * 送出感谢
   */
  sendThanks: function (e) {
    let that = this;
    console.log(e.detail.value.textarea);
    if (this.data.myVidios == '' && !this.data.checked) {
      wx.showToast({
        title: '请上传视频或选择感谢卡!',
        duration: 2000,
        icon: 'none'
      });
      return;
    }
    if (this.data.myVidios == '' && this.data.checked && this.data.banners.length == 0) {
      wx.showToast({
        title: '暂无感谢卡，请上传视频!',
        duration: 2000,
        icon: 'none'
      });
      return;
    }
    if (this.data.execData == null) {
      return;
    }
    let content = e.detail.value.textarea;
    if (content == '') {
      wx.showToast({
        title: '请输入感谢留言!',
        duration: 2000,
      });
      return;
    }
    let good = this.data.execData[this.data.index];
    if (this.data.order) {
      good = this.data.order;
    }
    let imgUrl = null;
    if (this.data.checked && this.data.banners.length > 0) {
      imgUrl = banners[this.data.cardIdx];
    }
    service.thankVideo(this.data.myVidios, good.id, good.goodsId, good.goodsName, good.sellerUserId, content, imgUrl,
      function (data) {
        if (data.count != 0) {
          that.dialog.show();
          that.setData({
            textValue: ' ',
            myVidios: '',
            count: data.count
          })
        } else {
          wx.redirectTo({
            url: '/mineSubs/pages/thanksVideo/thanksVideo?type=1',
          })
        }
      });
  },

  /**
   * 请求我的宝贝列表
   */
  requestBabyList: function () {
    let that = this;
    let param = new Object();
    param['userId'] = getApp().globalData.userId;
    param['status'] = 3;
    param['startPage'] = 0;
    babyService.requestProducts(param, function (data) {
      if ((!data.items || data.items.length == 0) && that.data.order == null) {
        wx.showModal({
          title: '温馨提示',
          content: '没有需要感谢的宝贝',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.navigateBack();
            }
          }
        });
        return;
      }
      that.setData({
        execData: data,
        good: data.items[that.data.index]
      });
    });
  },

  /**
   * 查询感谢卡
   */
  requestThankCardList: function () {
    let that = this;
    service.getThankCardList(3, function (data) {
      banners: data
    });
  },

  switchChange: function (e) {
    this.setData({
      checked: !this.data.checked
    })
    console.log('switch2 发生 change 事件，携带值为', e.detail.value)
  },

  cardClick: function (e) {
    let idx = e.currentTarget.dataset.idx;
    this.setData({
      cardIdx: idx
    })
  },

  /**
   * 点击领取果冻积分
   */
  receiveClick: function (e) {
    wx.redirectTo({
      url: '/mineSubs/pages/thanksVideo/thanksVideo?type=1',
    });
  }

})